﻿using MediatR;
using SheepCultivation.Common.Domain.SheepBasicInformation;
using SheepCultivation.Common.Domain.Week2;
using SheepCultivation.Common.Domain.Week3;
using SheepCultivation.Common.Dto;
using SheepCultivation.Common.Infrastructure.Interface;
using SheepCultivation.Enum;

namespace SheepCultivation.API.AddliCation.SheepManagement
{
    public class DeathManagementBackHandler : IRequestHandler<DeathManagementBackCommon, Result>
    {
        #region 仓储
        /// <summary>
        /// 基本信息仓储
        /// </summary>
        public readonly IBaseRepository<BasicInformation> BasicInformationRepository;
        /// <summary>
        /// 日志
        /// </summary>
        public readonly ILogger<DeathManagementBackHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="BasicInformationRepository">基本信息</param>
        /// <param name="logger">日志</param>
        public DeathManagementBackHandler(IBaseRepository<BasicInformation> BasicInformationRepository, ILogger<DeathManagementBackHandler> logger)
        {
            this.BasicInformationRepository = BasicInformationRepository;
            this.logger = logger;
        }
        #endregion

        #region 羊只死亡反填
        /// <summary>
        /// 羊只死亡反填
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> Handle(DeathManagementBackCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonEnumCode.error };
            //异常处理
            try
            {
                logger.LogInformation("羊只死亡反填成功!");
                var list = BasicInformationRepository.GetAll().FirstOrDefault(x => x.BreedingSheepArchiveId == request.BreedingSheepArchiveId);

                result.Code = (int)CommonEnumCode.success;
                result.Data = list;
                result.Msg = "羊只死亡反填成功";
            }
            catch (Exception ex)
            {
                logger.LogError("羊只死亡反填失败!");
                result.Msg = "羊只死亡反填失败";
            }
            return result;
        }
        #endregion
    }
}
